/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejbgame;

import ejb.Wordlist;
import ejb.WordlistFacadeRemote;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author automne
 */
public class WordlistControl {


    InitialContext context;
    WordlistFacadeRemote facade;

    public WordlistControl(String mappedName) {
        System.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
        try {
            context = new InitialContext();
            facade = (WordlistFacadeRemote) context.lookup(mappedName);
        } catch (NamingException ex) {
            Logger.getLogger(WordlistControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void create(String word) {
        Wordlist newWord = new Wordlist();
        newWord.setId(word);
        facade.create(newWord);
//        String s = "";
//        List<Wordlist> pp = facade.findAll();
//        for(Wordlist p : facade.findAll())
//        {
//            s += "Person: " + p.getId() + "\n";
//            System.out.println(s);
//        }
    }

    

    public boolean find(String word) {
        boolean isFounded = false;
        Wordlist newWord = facade.find(word.toLowerCase());
        if (newWord != null) {
            isFounded = true;
        } else {
            isFounded = false;
        }
        return isFounded;
    }

    public void init() {
        Wordlist newWord = new Wordlist();
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader("./wordlist.txt"));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(WordlistControl.class.getName()).log(Level.SEVERE, null, ex);
        }
        long i = 1;
        String tempInput = new String();
        try {
            tempInput = in.readLine();
        } catch (IOException ex) {
            Logger.getLogger(WordlistControl.class.getName()).log(Level.SEVERE, null, ex);
        }
        while (tempInput != null) {
            if (tempInput.length() <= 10 && tempInput.contains("'") == false) {
                newWord.setId(tempInput);
                facade.create(newWord);
            }
            try {
                tempInput = in.readLine();
            } catch (IOException ex) {
                Logger.getLogger(WordlistControl.class.getName()).log(Level.SEVERE, null, ex);
            }
            i++;
        }
    }
}
